const {createClient} = require('redis')
const process = require('process')

u0803 = [
    '301', '302', '401', '403', '511', '512', '600', '601'
]

u0804 = [
    '301', '303', '401', '404', '511', '522', '701', '702'
]

const main_key = 'user:id'

async function add_daily_user(client, thedate, ids) {
    let key = 'user:id:' + thedate
    await client.sAdd(key, ids)
}

async function daily_union(client, daily_set) {
    await client.sUnionStore(main_key, [main_key, daily_set])
}

async function calc_new_user(client, daily_set) {
    await client.sDiffStore("user:id:new", [daily_set, main_key])
}

async function calc_rem_user(client, previous_set, current_set) {
    await client.sInterStore('user:id:rem', [previous_set, current_set])
}

async function clear(client) {
    await client.del(['user:id', 'user:id:new', 'user:id:rem', 'user:id:20200803', 'user:id:20200804'])
}

async function run() {
    const client = createClient({
        url: 'redis://localhost:6379'
    })
    client.on('error', (err) => console.log('Redis Client Error', err));
    await client.connect();
    await clear(client);
    await add_daily_user(client, '20200803', u0803)
    await add_daily_user(client, '20200804', u0804)
    await daily_union(client, 'user:id:20200803')
    console.log('8月4日新增用户为：')
    await calc_new_user(client, 'user:id:20200804')
    let newuser = await client.sMembers('user:id:new')
    console.log(newuser)
    console.log('从8月3日到8月4日留存的用户：')
    await calc_rem_user(client, 'user:id:20200803', 'user:id:20200804')
    let remuser = await client.sMembers('user:id:rem')
    console.log(remuser)
    process.exit(0)
}

run().catch(err => console.log(err))